//
//  MyCouponViewController.m
//  LoveBeeProject
//
//  Created by 沈腾飞 on 2017/1/5.
//  Copyright © 2017年 斯芬克斯. All rights reserved.
//

#import "MyCouponViewController.h"
#import "MyCouponModel.h"
#import <Masonry.h>
#import "MyCouponTableViewCell.h"
static NSString * couponCellID = @"couponCellID";
static NSString * myCouponCellID = @"myCouponCellID";
@interface MyCouponViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak) UITableView *couponView;
@end

@implementation MyCouponViewController{
    NSArray *_newsList;
}

- (void)loadData{
    NSString *url = @"http://iosapi.itcast.cn/loveBeen/MyCoupon.json.php";
    NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"9" forKey:@"call"];
    [MyCouponModel loadNewsDataWithUrlStr:url parameters:parameters success:^(NSArray *newsList) {
        _newsList = newsList;
        [self.couponView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];


}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    UITableView *couponView = [[UITableView alloc] init];
    self.couponView = couponView;
    //设置数据源和代理
    couponView.delegate = self;
    couponView.dataSource = self;
    //自适应行高
    couponView.rowHeight = UITableViewAutomaticDimension;
    
    //预估行高
    couponView.estimatedRowHeight = 120;
    
    [couponView registerNib:[UINib nibWithNibName:@"MyCouponTableViewCell" bundle:nil] forCellReuseIdentifier:couponCellID];
    [couponView registerNib:[UINib nibWithNibName:@"MyCouponCell" bundle:nil] forCellReuseIdentifier:myCouponCellID];
    [self.view addSubview:couponView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
//    headerView.backgroundColor = [UIColor blackColor];
    self.couponView.tableHeaderView = headerView;
    
    UITextField *textF = [[UITextField alloc] init];
    textF.backgroundColor = [UIColor whiteColor];
    textF.borderStyle = UITextBorderStyleRoundedRect;
    textF.placeholder = @"请输入优惠券号码";
    [headerView addSubview:textF];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.layer.cornerRadius = 2.5;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"绑定" forState:UIControlStateNormal];
    btn.titleLabel.textColor = [UIColor blackColor];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:btn];
    [textF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.centerY.offset(0);
        make.right.equalTo(btn.mas_left).offset(-10);
        make.height.offset(30);
        
    }];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textF.mas_top);
        make.bottom.equalTo(textF.mas_bottom);
        make.right.offset(-10);
        make.width.offset(50);
    }];
    //布局
    [couponView mas_makeConstraints:^(MASConstraintMaker *make) {
        [make edges];
    }];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _newsList.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCouponModel *model = _newsList[indexPath.row];
    NSString *identifier;
    if (model.total_num) {
        identifier =myCouponCellID;
    }else{
        identifier = couponCellID;
    }
    
    MyCouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.model = model;
    return cell;
}


@end
